<?php
    Yii::import('application.components.BingTranslateLib.BingTranslateWrapper');
    class BingTranslateService extends FServiceBase
    {
        /**
        * Two Bing AppId for the translation.
        */
        //const APP_ID='E7C8C31171EE6E2768925057855C13DB5DBDD1D3',BACKUP_APP_ID='E7C8C31171EE6E2768925057855C13DB0DA5B7F7';
        /**
        * This service translate a plain text with a from-to language pair .
        * When the Bing AppId is invalid or expire, send an email to administator.
        * 
        * @param mixed $params
        * @return FServiceModel
        */
        public function getTranslatedText($params){
            $text=$this->getParam($params,'content','');
            $limit=$this->getParam($params,'limit',0);
            $text=$this->plainText($text,$limit);
            $gt=new BingTranslateWrapper(Settings::BING_API_ID1);
//            echo '<head>';
//            echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
//            echo '</head>';
//            echo ($text);die;
            if ($gt->validAppId() == 0) {
                $newGt = new BingTranslateWrapper(Settings::BING_API_ID2);
                if ($newGt->validAppId() != 0) {
                    try {
                        $text = stripslashes($newGt->translate(addslashes($text), 'vi', 'en'));
                    } catch (Exception $exc) {
                        echo $exc->getMessage();
                    }
                } else {
                    $this->sendEmailToAdmins();
                }
            }
            else
            {
                try {
                    $text = stripslashes($gt->translate(addslashes($text), 'vi', 'en'));
                } catch (Exception $exc) {
                    echo $exc->getMessage();
                }
            }
            $this->result->processed('text',$text);
            return $this->result;
        }
        /**
        * Send mail to Administators when appId is invalid or expires
        * 
        */
        private function sendEmailToAdmins(){
            Yii::import('application.modules.Core.extensions.vendors.mail.YiiMailMessage');
            Yii::app()->mail->viewPath = 'application.runtime.emails';
            $message = new YiiMailMessage;
            $message->view = 'bing_notify';
            $message->setSubject('[Vinatraders]: Your Bing appID is invalid or expired');
            //data
            $message->setBody(array(), 'text/html');
            $message->addTo('hung.nguyen@weblflexica.com');
            $message->addTo('thuan.nguyen@webflexica.com');
            if (defined('Settings::ADMIN_EMAIL'))
                $message->setFrom(array(Settings::ADMIN_EMAIL=>'Vinatraders.com Crawler'));
            Yii::app()->mail->send($message);
        }
        /**
        * Convert an encoded character to plain text, with limited character as params.
        * 
        * @param string $text
        * @param mixed $limit
        * @return string
        */
        private function plainText($text,$limit=0){
            $text = htmlspecialchars(
            html_entity_decode($text, ENT_QUOTES, 'UTF-8'), 
            ENT_QUOTES, 'UTF-8'
            );
            if($limit>0)  
                $text=firstWordsExt($text,$limit);
            return $text;
        }
    }
?>
